<?php

namespace app\common\model;
use think\Db;
//use think\Model;

class BalanceRecordModel extends BaseModel{

    protected $table = 'tp_balance_record';

    /*
    添加资金变动记录
    */
    public function addRecord($uid,$wid,$balance,$remark,$type=0,$order_id,$order_remark){
        $user_balance = $this ->where('uid',$uid)
                                ->where('wid',$wid)
                                ->order('id desc')
                                ->value('after_balance')?:0;
        $after_balance = $user_balance + $balance;
        if ($after_balance < 0) {
            $res = 0;
        } else {
            $res = $this->insert([
                "uid"            => $uid,
                "wid"            => $wid,
                "type"           => $type,
                "before_balance" => $user_balance,
                "balance"        => $balance,
                "after_balance"  => $after_balance,
                "remark"         => $remark,
                "order_id"       =>$order_id,
                "order_remark"   =>$order_remark,
                "date"           => $this->getTime(),
                'change_date'    =>date('Y-m-d H:i:s')
            ]);
        }
        return $res;
    }
}